
const {ccclass, property} = cc._decorator;

@ccclass
export default class DWC_DanMuItem extends cc.Component {

    @property(cc.Sprite)
    emojSp:cc.Sprite = null;

    @property(cc.SpriteAtlas)
    emojSpAtlas:cc.SpriteAtlas = null;

    // @property(cc.Sprite)
    // bg: cc.Sprite = null;

    // @property(cc.SpriteFrame)
    // bgSpf:cc.SpriteFrame = null;

    @property(cc.Label)
    lbMsg:cc.Label = null;

    @property(cc.Float)
    minHeight = 0;

    @property(cc.Float)
    maxHeight = 400;

    _msg = null;
    moveTime = 0;
    moveLen = 0;
    allTime = 0 ;
    DanMuCom = null;
    start () {

    }

    init(msgData,time,len,DanMuCom){

        this._msg = msgData;
        this.moveTime = time;
        this.moveLen = len;
        this.allTime = time;
        this.DanMuCom = DanMuCom;
        this.setHeight();
        this.initUI();
    }

    initUI(){
        // let self = cc.vv.session.getRid() == this._msg["rid"];
        this.initContent();
        //this.bg.spriteFrame = self ? this.bgSpf : null;

        this.setLabelColor();
    }

    setLabelColor(){
        let type = this._msg["type"];
        if(type == 1){
            //私聊
            this.lbMsg.node.color = new cc.Color(252,252,68,200);
        }else if(type == 2){
            //桌子
            this.lbMsg.node.color = new cc.Color(15,218,104,200);
        }else if(type == 3){
            //大厅
            this.lbMsg.node.color = new cc.Color(255,255,255,200);
        }else {
            //大厅
            this.lbMsg.node.color = new cc.Color(255,255,255,200);
        }
    }

    initContent(){
        //提示
        if(this._msg["tip"]){
            this.lbMsg.string = `${this._msg["rolename"]}进入游戏`;
        }else {
            let str = this._msg["content"];
            if( str.search("@emoticon_") !== -1 ){
                let strSlice = str.slice(10)
                let spf = this.emojSpAtlas.getSpriteFrame(strSlice);
                if(spf){
                    this.emojSp.spriteFrame = spf;
                    this.emojSp.node.active = true;
                    this.lbMsg.node.active = false;
                }else {
                    this.lbMsg.string =str;
                    this.emojSp.node.active = false;
                    this.lbMsg.node.active = true;
                }
            }else {
                this.lbMsg.string =str;
                this.emojSp.node.active = false;
                this.lbMsg.node.active = true;
            }
        }

    }

    setHeight(){

        let height = Math.random()*(this.maxHeight - this.minHeight) + this.minHeight;
        this.node.y = height;
        this.node.x = 0;
    }

    update(dt){
        if(this.moveTime > 0){
            this.moveTime -= dt;
            this.node.x -= (dt / this.allTime) * this.moveLen;

            if(this.moveTime <= 0){
                this.DanMuCom && this.DanMuCom.pushDanMuItem(this.node);
            }
        }
    }

}
